﻿#region Header

/*
Behavioral Rating of Dancing Human Crowds based on Motion Patterns
By

Pascal Hauser 
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

and

Raphael Gfeller
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

*/

#endregion

#region Usings

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading;
using Sebarf.Services.Interfaces;

#endregion

namespace paravili.Services {
	/// <summary>
	/// Description of the class
	/// </summary>
	public class StatisticValuesLocatorProviderService : Service, IStatisticValuesLocatorService,
														 IStatisticValuesProviderService {
		#region Public Properties

		[ServiceRequest]
		public IDispatcherLocatorService DispatcherLocatorService { get; set; }

		#endregion

		#region Public Methods

		public StatisticValuesLocatorProviderService() {
			StatisticsValues = new ObservableCollection<StatisticValueEntry>();
		}

		#endregion

		#region Private Fields

		private readonly Dictionary<string, StatisticValueEntry> m_entryById = new Dictionary<string, StatisticValueEntry>();

		#endregion

		#region IStatisticValuesLocatorService Members

		public ObservableCollection<StatisticValueEntry> StatisticsValues { get; set; }

		public StatisticValueEntry this[string key] {
			get {
				if (m_entryById.ContainsKey(key)) {
					return m_entryById[key];
				}
				return null;
			}
		}

		public event EventHandler<StatisticValuesLocatorServiceEventArgs> StatisticValueEntryAdded;

		#endregion

		#region IStatisticValuesProviderService Members

		public void SetStatisticValue(string id, object value) {
			DispatcherLocatorService.GetDispatcher().Invoke(new ThreadStart(delegate {
				if (m_entryById.ContainsKey(id)) {
					m_entryById[id].Value = value;
				}
				else {
					var entryToAdd = new StatisticValueEntry { Name = id, Value = value };
					m_entryById.Add(id, entryToAdd);
					StatisticsValues.Add(entryToAdd);
					if (StatisticValueEntryAdded != null) {
						StatisticValueEntryAdded(this,
												  new StatisticValuesLocatorServiceEventArgs { Entry = entryToAdd });
					}
				}
			}), null);
		}

		#endregion
	}
}